package Sever;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import sun.util.logging.resources.logging;

import libarary.Contants;

/**
 * 
 * @author ADMIN
 */
public class Server implements Contants, Runnable {

	ServerSocket server;
	int port;
	private Thread thread = null;
	public static Vector<ThreadSocket> v = new Vector<ThreadSocket>(1, 3);
	public static Vector<timethear> vt = new Vector<timethear>(1, 3);
	public static Vector<String> listUser = new Vector<String>(1, 3);
	int nuberCL = 0;

	/** Creates a new instance of Server */

	public Server(int port) {
		this.port = port;
		try {
			server = new ServerSocket(this.port);
			if (thread == null) {
				thread = new Thread(this);
				thread.start();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void sendata(String st, String threadname) {
		int i = checkName(threadname);
		v.elementAt(i).sendData(st);
	}

	public void addClient(Socket socket) {
		v.add(new ThreadSocket(this, socket));
		v.elementAt(v.size() - 1).setName(ThreadSocket.us.getName());
		vt.add(new timethear(this, socket));
		vt.elementAt(vt.size() - 1).setName(ThreadSocket.us.getName());
		try {
			v.elementAt(v.size() - 1).open();
			vt.elementAt(v.size() - 1).opened();
			v.elementAt(v.size() - 1).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int checkName(String name) {
		for (int i = 0; i < v.size(); i++)
			if (name.equals(v.elementAt(i).getName()))
				return i;
		return -1;
	}

	public void thoat(String name) {
		int i = checkName(name);
		try {
			// v.elementAt(i).sendData("exit");
			v.elementAt(i).socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		v.removeElementAt(i);
		String kq = state.noichuoi;
		listUser = User.getAllUserOnl();
		for (int j = 0; j < listUser.size(); j++) {
			kq += listUser.elementAt(j).toString() + state.noichuoi;
		}
		for (int j = 0; j < v.size(); j++)
			v.elementAt(j).sendData(state.update + kq);
	}

	public void login(String name) {
		int i = checkName(name);
		System.out.println("stt" + i);
		System.out.println(name);
		String kq = state.noichuoi;
		listUser = User.getAllUserOnl();
		for (int j = 0; j < listUser.size(); j++) {
			kq += listUser.elementAt(j).toString() + state.noichuoi;
		}
		System.out.println(kq);
		for (int j = 0; j < v.size(); j++)
			if (i != j)
				v.elementAt(j).sendData(state.update + kq);
			else
				v.elementAt(j).sendData(state.Login + name + kq);
	}
	//update diem
	public void updatescore(){
		listUser=User.getallScore();
		String kq = state.noichuoi;
		for (int j = 0; j < listUser.size(); j++) {
			kq += listUser.elementAt(j).toString() + state.noichuoi;
		}
		for (int j = 0; j < v.size(); j++){
			v.elementAt(j).sendData(state.gamescore + kq);
		}
		
	}
	//-------------cap nhap diem----------------------
	public void gameover(String name,String kq){
		int i = checkName(name);
		v.elementAt(i).us.updateScore(Integer.parseInt(kq));
		v.elementAt(i).sendData(state.over+kq);
		vt.elementAt(i).stop();
	}
	public void chatRoom(String st) {
		for (int j = 0; j < v.size(); j++)
			v.elementAt(j).sendData(state.updatechat + st);
	}

	public void playgame(String name) {
		int i = checkName(name);
		v.elementAt(i).sendData(state.playgame + "");

		if (v.elementAt(i).playge == 0) {
			timemer(name);
			v.elementAt(i).playge = 1;
		} else if (v.elementAt(i).playge == 1) {
			vt.elementAt(i).tt = 1;
			v.elementAt(i).playge = 2;
			
		} else if(v.elementAt(i).playge == 2){
			vt.elementAt(i).tt = 0;
			v.elementAt(i).playge = 1;
		
		}

	}

	public void timemer(String name) {
		int i = checkName(name);
		vt.elementAt(i).setMini(0, 10);
		vt.elementAt(i).start();
	}

	public static void main(String args[]) {
		new Server(7000);

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while (true) {
				addClient(server.accept());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// --------------------------
	class timethear extends Thread {
		Socket socket;
		Server server;
		DataOutputStream dos;
		DataInputStream din;
		public User us;
		int ip;
		int mini, sec;
		boolean loop = true;
		public int tt = 0;

		public timethear(Server server, Socket pSocket) {
			this.socket = pSocket;
			ip = pSocket.getPort();
			this.server = server;
			us = new User();
		}

		public void setMini(int mini, int sec) {
			this.mini = mini;
			this.sec = sec;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			while (loop) {

				try {
					if (socket.isClosed() == false) {
						if (tt == 0) {
							
							sec--;
							if (sec == -1) {
								sec = 59;
								mini--;
							}
						}
						if (mini == 0 && sec==0)
							loop = false;
						this.sendData(state.time + state.noichuoi + mini
								+ state.noichuoi + sec);
						Thread.sleep(900);
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			this.sendData(state.gameover+"");
		}

		public void opened() {
			try {
				dos = new DataOutputStream(socket.getOutputStream());
				din = new DataInputStream(socket.getInputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		public void sendData(String st) {
			try {
				if (socket != null) {
					dos.writeUTF(st);
					dos.flush();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

		public String receiveData() {

			try {
				if (din != null)
					return din.readUTF().toString();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			return null;
		}

		public void open() {
			try {
				dos = new DataOutputStream(socket.getOutputStream());
				din = new DataInputStream(socket.getInputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}
